/*
============================================================================
 Name        : BlockLogin.cpp
 Author      : 
 Version     :
 Copyright   : 
 Description : CBlockLogin implementation
============================================================================
*/

#include "BlockLogin.h"
#include "eStockappui.h"
#include "Global.h"
#include "BlockMenu.h"
#include "Control.h"
#include <stringloader.h>
#include <eStock_L2HY.rsg>
#include "BlockMemo.h"
#include "eStockMainGridContainer.h"

CBlockLogin::CBlockLogin(CBaseBlock* bsBack , const TInt& nType):
CBlock(bsBack),
m_nType(nType),
m_nCurrent(0)
{
}

CBlockLogin::~CBlockLogin()
{
	for(TInt i=0; i<3; i++)
		delete m_Ctrls[i];

	if(m_strArray != NULL)
	{
		m_strArray->Reset();
		delete m_strArray;
	}
}

CBlockLogin* CBlockLogin::NewLC(CBaseBlock* bsBack , const TInt& nType)
{
	CBlockLogin* self = new (ELeave)CBlockLogin(bsBack,nType);
	CleanupStack::PushL(self);
	self->ConstructL();
	return self;
}

CBlockLogin* CBlockLogin::NewL(CBaseBlock* bsBack , const TInt& nType)
{
	CBlockLogin* self=CBlockLogin::NewLC(bsBack,nType);
	CleanupStack::Pop(); // self;
	return self;
}

void CBlockLogin::ConstructL()
{
	CBlock::ConstructL();

	CGlobal::GetDesFromID(m_strOK,LOC_BUF_MENU_OPTION);
	CGlobal::GetDesFromID(m_strCancel,LOC_BUF_MENU_CANCEL2);

	CGlobal::GetDesFromID(m_strTitle,LOC_BUF_MENU_LOGIN);
		
	m_strArray = new(ELeave)CDesC16ArrayFlat(10);

	TBuf<16> temp;
	CGlobal::GetDesFromID(temp,LOC_BUF_STR_PHONE);
	m_inputphone = CSingleLineTextInput::NewL(temp, 11);
	m_inputphone->SetText(CGlobal::Static()->m_strUserName);
	CGlobal::GetDesFromID(temp,LOC_BUF_STR_INPUTPHONE);
	m_inputphone->SetEmtpyString(temp);
	m_Ctrls[0] = m_inputphone;

	CGlobal::GetDesFromID(temp,LOC_BUF_STR_PASSWORD);
	m_inputpw =  CSingleLineTextInput::NewL(temp, 6);
	m_inputpw->SetText(CGlobal::Static()->m_strPassword);
	CGlobal::GetDesFromID(temp,LOC_BUF_STR_INPUTPW);
	m_inputpw->SetEmtpyString(temp);
	m_inputpw->SetInputTpye(CTextInput::PASSWORD);
	m_Ctrls[1] = m_inputpw;

	CGlobal::GetDesFromID(temp,LOC_BUF_STR_REMEMBER);
	m_checkbox = CCheckBox::NewL(temp);
	m_checkbox->m_bLock = CGlobal::Static()->m_nLock>0?ETrue:EFalse;
	m_Ctrls[2] = m_checkbox;

	menuBar->m_subMenu.ResetAndDestroy();
	menuBar->Append(CPopMenu::NewL(KMenuIdLogin,LOC_BUF_MENU_LOGIN2));
	//menuBar->Append(CPopMenu::NewL(KMenuIdGetPasswd,LOC_BUF_STR_GETPASSWORD));

	if (CGlobal::Static()->m_osMajor == 3)
		m_Ctrls[0]->SetFocus(ETrue);
}

void CBlockLogin::SizeChanged()
{
	CBlock::SizeChanged();
	for (int i = 0; i<3; i++)
	{
		TInt y = (i+1)*(m_nLineHeight+4);
		m_Ctrls[i]->SetRect(0, y, m_nScreenWidth, y+m_nLineHeight-1);
	}

	HBufC* strNotice = NULL;
	/*if (m_nType == -10)//\x{5b9e}\x{540d}\x{7528}\x{6237}\x{533f}\x{540d}\x{767b}\x{5f55}\x{65f6}
	{
		strNotice = StringLoader::LoadLC(LOC_BUF_STR_LOGINNOTICE_ANONY);
	} 
	else*/
	{
		//if(m_pGlobal->g_nRegAllowed == 0)
		strNotice = StringLoader::LoadLC(LOC_BUF_STR_LOGINNOTICENOREG);
		//else
		//	strNotice = StringLoader::LoadLC(LOC_BUF_STR_LOGINNOTICE);
	}
	CreateTipArray(strNotice->Des(),*m_strArray);
	CleanupStack::PopAndDestroy(strNotice);
}

void CBlockLogin::DrawTitle(CFbsBitGc& g, TRect aRect, TInt aBaseLine) const
{
	if (m_nCurrent==0 || m_nCurrent==1)
	{
		TInt nMax;
		TInt nLen;
		if (m_nCurrent==0)
		{
			nMax = 11;
			nLen = m_inputphone->GetTextLength();
		}
		else
		{
			nMax = 6;
			nLen = m_inputpw->GetTextLength();
		}
		TBuf<10> temp;
		temp.Format(_L("(%d/%d)"),nLen,nMax);
		m_strTitle.Append(temp);
	}

	g.DrawText(m_strTitle, aRect, aBaseLine);
}

void CBlockLogin::OnDraw(CFbsBitGc& g) const
{
	for(TInt i=0; i<3; i++)
		m_Ctrls[i]->Draw(g);

	TInt nY = m_checkbox->m_rect.iBr.iY;
	for(TInt i=0; i<m_strArray->Count(); i++)
	{
		CGlobal::DrawString(g,(*m_strArray)[i],0,nY,TOP|LEFT,CGlobal::Static()->g_rgbText);
		nY += m_nLineHeight;
	}
}

TBool CBlockLogin::OnMenuEvent(TInt menuId)
{
	if (menuId == KMenuIdLogin)
	{
		if(m_inputphone->GetTextLength()>0 && m_inputphone->GetTextLength() < 11)
		{
			TBuf<32> buf;
			CGlobal::GetDesFromID(buf,LOC_BUF_STR_ERRPHONE);
			ShowAlert(buf, this);
		}
		else
		{
			m_inputphone->GetText(m_pGlobal->m_strUserName);
			m_inputpw->GetText(m_pGlobal->m_strPassword);
			m_pGlobal->m_nLock = m_checkbox->m_bLock;

			m_pGlobal->m_bLogined = EFalse;
			GetContainer()->m_blockMenu->m_bJustIO = ETrue;
			if(eStockUI->GetCurView() && eStockUI->GetCurView()->iContainer)
				eStockUI->GetCurView()->iContainer->DelBlock();
		}
	}
	else if (menuId == KMenuIdGetPasswd)
	{
		CBlock* regBlock = NULL;
		if (m_pGlobal->g_strSMSChannel1 != NULL && m_pGlobal->g_strSMSChannel2 != NULL )
		{
			TBuf<128> buf;
			TBuf<128> formatbuf;
			CGlobal::GetDesFromID(formatbuf,LOC_BUF_STR_GETPWDINFO);
			buf.Format(formatbuf,m_pGlobal->g_strSMSChannel1);
			regBlock = CBlockMemo::NewL(NULL,208,buf);
		} 
		else
		{
			regBlock = CBlockMemo::NewL(NULL,208,KNullDesC);
		}
		GetContainer()->AddBlock(regBlock);
		//GetContainer()->AddBlock(CBlockMemo::NewL(NULL,208,KNullDesC));
	}
	return EFalse;
}

void CBlockLogin::OnKeyEvent(const TInt& keyCode)
{
	if (keyCode==m_pGlobal->g_nKeyOK || keyCode==EStdKeyNkpAsterisk || keyCode == '*')
	{
		menuBar->SetPopup(ETrue);
		DrawNow();
		return;
	}
	else if (keyCode==m_pGlobal->g_nKeySelect)
	{
		if (m_nCurrent == 2)
		{
			m_Ctrls[m_nCurrent]->OnKeyDown(keyCode);
			DrawNow();
		}
		else
		{
			OnMenuEvent(KMenuIdLogin);
		}
		return;
	}
	else if (keyCode==m_pGlobal->g_nKeyCancel)
	{
		if(eStockUI->GetCurView() && eStockUI->GetCurView()->iContainer)
			eStockUI->GetCurView()->iContainer->DelBlockNoSocket();
		return;
	}
	else if (keyCode==m_pGlobal->g_nKeyUp || keyCode==m_pGlobal->g_nKeyDown)
	{
		m_Ctrls[m_nCurrent]->SetFocus(EFalse);
		if (keyCode==m_pGlobal->g_nKeyUp)
		{
			m_nCurrent--;
			if (m_nCurrent<0)
				m_nCurrent =2;
		}
		else
		{
			m_nCurrent++;
			if (m_nCurrent>2)
				m_nCurrent = 0;
		}

		m_Ctrls[m_nCurrent]->SetFocus(ETrue);
		DrawNow();
		return;
	}
	if(m_Ctrls[m_nCurrent]->OnKeyDown(keyCode))
	{
		DrawNow();
		return;
	}
}

void CBlockLogin::OnMouseUp(const TPoint& aPt)
{
	TInt nPos = -1;
	for(TInt i=0; i<3; i++)
	{
		if(m_Ctrls[i]->m_rect.Contains(aPt))
		{
			nPos = i;
			break;
		}
	}
	if (nPos>=0)
	{
		m_Ctrls[m_nCurrent]->SetFocus(EFalse);
		m_nCurrent = nPos;
		m_Ctrls[m_nCurrent]->SetFocus(ETrue);
		if (m_nCurrent == 0)
			m_kb->SetInputType(CKeyBoard::NUMERIC);
		if (m_nCurrent == 1)
			m_kb->SetInputType(CKeyBoard::PASSWORD);
		if (m_nCurrent == 2)
		{
			TInt n = m_nLineHeight/2;
			TInt nx = m_checkbox->m_nXLock;
			TInt ny = m_checkbox->m_nYLock;
			if (aPt.iX>=nx&&aPt.iX<=nx+n && aPt.iY>=ny&&aPt.iY<=ny+n)
				OnKeyEvent(m_pGlobal->g_nKeySelect);
		}
		else
			DrawNow();
	}
}

void CBlockLogin::OnInput(const TDesC8& strInput)
{
	if (m_kb->m_bInputGoodCode)
	{
		CBlock::OnInput(strInput);
		return;
	}
	
	TBuf<32> buf;
	buf.Copy(strInput);
	static_cast<CSingleLineTextInput*>(m_Ctrls[m_nCurrent])->SetText(buf);
	DrawNow();
}
